package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.BaseServlet;
import common.CartShop;
import bean.User;

/**
 * Servlet implementation class MainServlet
 */
@WebServlet("/Main")
public class MainServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MainServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	@Override
	public String basefun(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("进入首页");
		request.setAttribute("requestURI", null);
		HttpSession session=request.getSession();
		session.setMaxInactiveInterval(3600);
		CartShop cartShop=(CartShop) session.getAttribute("cartShop");
		//如果是第一次进入首页，创建一个新的购物车对象，用户id为0，保存在session中
		if(cartShop==null) {
			System.out.println("创建新的购物车对象，用户id=0");
			cartShop=new CartShop(0);
			session.setAttribute("cartShop", cartShop);
		}
		//根据判断购物车为空存入ifEmpty
		session.setAttribute("ifEmpty",cartShop.ifEmpty());
		//获取用户对象，空则为未登录
		if(session.getAttribute("user")==null) {
			System.out.println("未登录==0");
			session.setAttribute("ifLogin", 0);
		}
		else {
			System.out.println("登录==1");
			session.setAttribute("ifLogin", 1);
		}
		return "f:jsp/Main";
	}
}
